/**
 * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * For the applicable distribution terms see the license.txt -file, included in
 * the distribution.
 */

#include "myqmlapplicationviewer.h"
#include <QTimer>
#include <QGraphicsObject>
#include <QDebug>

#if defined(Q_OS_SYMBIAN)
#include <aknedsts.h>
#include <coeaui.h>
#include <coemain.h>
#include <w32std.h>
#define EAknEditorFlagEnablePartialScreen 0x200000
#endif

MyQmlApplicationViewer::MyQmlApplicationViewer(QWidget *parent) :
    QmlApplicationViewer(parent), audioPlayer(new AudioPlayer(parent))
{
#if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
    rotationData = new RotationData(parent);
    deviceInfo = new QSystemDeviceInfo(parent);
    audioPlayer->profile(deviceInfo->currentProfile());
#endif
}

void MyQmlApplicationViewer::connectSignals()
{
    QObject *qmlObject = rootObject();
    QObject::connect(qmlObject, SIGNAL(speedChanged(qreal)), audioPlayer, SLOT(setSpeed(qreal)));
    QObject::connect(qmlObject, SIGNAL(stopAudio()), audioPlayer, SLOT(stop()));
    QObject::connect(qmlObject, SIGNAL(startAudio()), audioPlayer, SLOT(start()));
    QObject::connect(qmlObject, SIGNAL(splitVKB()), SLOT(enableSplitSreen()));
#if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
    QObject::connect(rotationData, SIGNAL(rotationChanged(QVariant)), qmlObject, SLOT(updateControls(QVariant)));
    QObject::connect(rotationData, SIGNAL(tiltedLeft()), qmlObject, SLOT(tiltLeft()));
    QObject::connect(rotationData, SIGNAL(tiltedRight()), qmlObject, SLOT(tiltRight()));
    QObject::connect(deviceInfo, SIGNAL(currentProfileChanged(QSystemDeviceInfo::Profile)), audioPlayer, SLOT(profile(QSystemDeviceInfo::Profile)));
#endif
}

void MyQmlApplicationViewer::showSplashScreen()
{
    setMainQmlFile(QLatin1String("qml/MotoTrial/SplashScreen.qml"));
    showExpanded();
    QTimer::singleShot(0, this, SLOT(loadGame()));
}

void MyQmlApplicationViewer::loadGame()
{
    setMainQmlFile(QLatin1String("qml/MotoTrial/main.qml"));
    showExpanded();
    connectSignals();
}

void MyQmlApplicationViewer::enableSplitSreen()
{
#if defined(Q_OS_SYMBIAN)
    MCoeFepAwareTextEditor *fte = CCoeEnv::Static()->AppUi()->InputCapabilities().FepAwareTextEditor();

    // FepAwareTextEditor() returns 0 if no text editor is present
    if (fte)
    {
        qDebug() << "Text editor is present";
        CAknEdwinState *state = STATIC_CAST(CAknEdwinState*, fte->Extension1()->State(KNullUid));
        state->SetFlags(state->Flags() | EAknEditorFlagEnablePartialScreen);
    }
    else
        qDebug() << "Text editor is NOT present";
#endif
}
